package com.jiaquyun.jcps.ui.mine

import android.app.Activity
import android.content.Intent
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.TimeUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.jiaquyun.jcps.R
import com.jiaquyun.jcps.base.ViewModeActivity
import com.jiaquyun.jcps.entity.SettleEntity
import com.jiaquyun.jcps.mode.vm.MineViewModel
import com.jiaquyun.jcps.popup.ConfirmCancelPopup
import com.lemlin.network.HttpServiceStatus
import kotlinx.android.synthetic.main.activity_settle_record.*

class SettleRecordActivity : ViewModeActivity<MineViewModel>() {

    companion object {
        fun open(activity: Activity) {
            val intent = Intent(activity, SettleRecordActivity::class.java)
            activity.startActivity(intent)
        }
    }

    private val settleRecordAdapter =
        object : BaseQuickAdapter<SettleEntity, BaseViewHolder>(R.layout.item_settle_record) {
            init {
                addChildClickViewIds(R.id.tvSettleRecordStatus)
            }

            override fun convert(holder: BaseViewHolder, item: SettleEntity) {
                holder.setText(
                    R.id.tvSettleRecordDate,
                    TimeUtils.millis2String((item.createtime * 1000), ("yyyy-MM-dd"))
                )
                    .setText(R.id.tvSettleRecordPrice, item.price)
                    .setText(
                        R.id.tvSettleRecordStatus, when (item.status) {
                            1 -> "待处理"
                            2 -> "已结算"
                            else -> "审核退回"
                        }
                    )
                    .setTextColorRes(R.id.tvSettleRecordStatus,when (item.status) {
                        1 -> R.color.FF_0000FF
                        2 -> R.color.FF_008000
                        else -> R.color.FF_FF0000
                    })
            }
        }


    private val confirmCancelPopup: ConfirmCancelPopup by lazy {
        ConfirmCancelPopup(
            context = this,
            onConfirm = { any: Any?, action: ConfirmCancelPopup.Action? ->
                confirmCancelPopup.dismiss()
                if (action == null)
                    return@ConfirmCancelPopup
                if (action == ConfirmCancelPopup.Action.CONFIRM_SETTLE) {

                }
            },
            onCancel = { _: Any?, _: ConfirmCancelPopup.Action? ->
                confirmCancelPopup.dismiss()
            })
    }

    override fun viewModel(): MineViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_settle_record

    override fun stateBar(): StatusBar {
        return StatusBar(isLightMode = true, color = R.color.colorWhite, view = flSettleRecordBar)
    }


    override fun initView() {

        ivSettleRecordBack.setOnClickListener {
            finish()
        }

        rvSettleRecordBody.adapter = settleRecordAdapter

        settleRecordAdapter.setOnItemChildClickListener { adapter, view, position ->
            val itemData: SettleEntity = settleRecordAdapter.getItem(position)
            if (view.id == R.id.tvSettleRecordStatus) {
                if (itemData.status != 3)
                    return@setOnItemChildClickListener
                confirmCancelPopup.setMessage("退回原因：${itemData.note}")
                    .setAction(ConfirmCancelPopup.Action.CONFIRM_SETTLE)
                    .setAny(null)
                    .showPopupWindow()
            }
        }

        smartRefreshHeader.setColorSchemeResources(R.color.colorMain)

        smartRefreshLayout.setOnRefreshListener {
            viewModel.cashLog()
        }

        smartRefreshLayout.setOnLoadMoreListener {
            viewModel.cashLog(isLoadMore = true)
        }

    }

    override fun initData() {
        smartRefreshLayout.autoRefresh()
    }

    override fun initViewMode() {

        viewModel.settleDataLiveData.observe(this, Observer { response ->
            if (response.statusCode == HttpServiceStatus.Status.FAILURE) {
                smartRefreshLayout.finishRefresh(false)
                smartRefreshLayout.finishLoadMore(false)
                toast(response.statusTip)
            }
            if (response.statusCode == HttpServiceStatus.Status.SUCCESS) {
                smartRefreshLayout.finishRefresh()
                smartRefreshLayout.finishLoadMore()
                response.httpData?.let {
                    if (it.loadMore) settleRecordAdapter.addData(it.data)
                    else settleRecordAdapter.setList(it.data)
                }
            }
        })
    }
}